package ali;

import java.lang.Math;
import java.util.Scanner;

/*
 * @Author: qianmingtian
 * @Date: 2020-05-29 15:37:44
 * @LastEditors: qianmingtian
 * @LastEditTime: 2020-07-08 09:31:14
 * @FilePath: \qianmingtian.github.io\code\ali\AliAfter.java
 */
public class AliAfter {

    public static double count(int n, int m, int w2, int w3) {
        double cost = Double.MAX_VALUE;

        // 最多用多少2和3
        double low = Math.ceil(Math.log(m / n) / Math.log(3));
        double high = Math.ceil(Math.log(m / n) / Math.log(2));

        for (double i = low; i <= high; i++) {
            for (double j = 0; j <= i; j++) {
                if ((Math.pow(3, i - j) * Math.pow(2, j)) * n >= m) {
                    double costTem = (i - j) * w3 + w2 * j;
                    if (costTem < cost) {
                        cost = costTem;
                    }
                }
            }
        }
        // for (int i = 0; i < high && i low; i++) {
        // if(Math.pow(2, (high-i))*3>= m){
        // cost =(int) (high-i)*w2 + w3;
        // }

        // }

        // System.out.println(low + " " + high);

        return cost;

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int size = in.nextInt();

        int[] results = new int[size];

        for (int i = 0; i < size; i++) {

            int start = in.nextInt();
            int target = in.nextInt();
            int cost1 = in.nextInt();
            int cost2 = in.nextInt();
            results[i] = (int) count(start, target, cost1, cost2);
            System.out.println(results[i]);
        }

        in.close();
    }
}